﻿
// ChatClientDlg.cpp: 实现文件
//

#include "pch.h"
#include "framework.h"
#include "ChatClient.h"
#include "ChatClientDlg.h"
#include "afxdialogex.h"
#include"CClientSocket.h"//不能使用class C....；
#include"CHistory.h"
#include"CMplayer.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif

#define PORT 5555
// CChatClientDlg 对话框



CChatClientDlg::CChatClientDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_CHATCLIENT_DIALOG, pParent)
	, m_strSendMsg(_T(""))
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_pClientSocket = NULL;
}

void CChatClientDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);

	DDX_Text(pDX, IDC_EDIT2, m_strSendMsg);

	DDX_Control(pDX, IDC_EDIT2, m_sendmeg);
	DDX_Control(pDX, IDC_LIST1, m_list1);
}

BEGIN_MESSAGE_MAP(CChatClientDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTN_SEND, &CChatClientDlg::OnBnClickedBtnSend)
	ON_BN_CLICKED(IDC_BUTTON1, &CChatClientDlg::OnBnClickedButton1)
	ON_BN_CLICKED(IDC_BUTTON2, &CChatClientDlg::OnBnClickedButton2)
	ON_BN_CLICKED(IDC_BUTTON3, &CChatClientDlg::OnBnClickedButton3)
END_MESSAGE_MAP()


// CChatClientDlg 消息处理程序

BOOL CChatClientDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码
	m_pClientSocket = new CClientSocket;
	//创建套接字
	CString str;
	if (FALSE == m_pClientSocket->Create())//不能将端口写成固定的
	{
		str.Format("创建套接字失败。错误代码%d", GetLastError());//给str赋值
		MessageBox(str, "温馨提示", MB_OK);
		delete m_pClientSocket;
		m_pClientSocket = NULL;
		EndDialog(IDOK);//关闭对话框
		return TRUE;
	}
	//链接服务器；
	if (false == m_pClientSocket->Connect("10.7.136.125",PORT))//右边的是端口号，左边的是IP地址  （注意IP地址，若IP地址错误会出现10060错误）
	{
		str.Format("创建套接字失败2。错误代码%d", GetLastError());//给str赋值
		MessageBox(str, "温馨提示", MB_OK);
		delete m_pClientSocket;
		m_pClientSocket = NULL;
		EndDialog(IDOK);
		return TRUE;
	}
	int nStyle = m_list1.GetExtendedStyle();
	nStyle = LVS_EX_FULLROWSELECT;
	m_list1.SetExtendedStyle(nStyle);
	CRect rect;
	m_list1.GetClientRect(&rect);

	m_list1.InsertColumn(0, _T("                                你们已经"), LVCFMT_RIGHT, rect.Width() / 2);
	m_list1.InsertColumn(1, _T("成为朋友了"), LVCFMT_LEFT, rect.Width() / 2);
	
	//ShowWindow(SW_MINIMIZE);
	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CChatClientDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CChatClientDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}



//CString m_NewString;
int i = 0;
void CChatClientDlg::OnBnClickedBtnSend()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);//将值赋给变量
	
	if (m_strSendMsg.IsEmpty())
	{
		MessageBox("消息为空，发送失败","提示");
		return;

	}

	if (m_strSendMsg.GetLength() >= 512)//1024字节
	{
		MessageBox("消息过长，发送失败","提示");
		return;

	}
	/*m_NewString += "yongsheng";
	m_NewString += "\r\n";
	m_NewString = m_NewString+m_strSendMsg;
	m_NewString += "\r\n";*/
	m_pClientSocket->Send(m_strSendMsg, m_strSendMsg.GetLength()*2 ); //发给了服务器,GetLength()获取的是字符个数
	
	m_list1.InsertItem(i,_T( "涌盛"));
	i++;
	m_list1.InsertItem(i, m_strSendMsg);
	i++;
    // m_SendEdit.SetWindowText(_T("王涌盛"));
	//m_SendEdit.SetWindowText(_T("\r\n"));
	//m_SendEdit.SetSel(10, 512);
	
	//m_SendEdit.ReplaceSel(_T("\r\n"));
	//m_SendEdit.ReplaceSel(m_strSendMsg);

	//m_strSendMsg += "\r\n";
	//m_SendEdit.ReplaceSel(_T("\r\n"));
	/*m_SendEdit.SetSel(0, -1);
	m_SendEdit.Clear();*/
	//m_SendEdit.SetWindowText(_T(""));
	/*m_SendEdit.SetWindowTextW(_T("王涌盛"));
	m_SendEdit.SetWindowTextW(m_strSendMsg);*/
	
	m_sendmeg.SetWindowText(_T(""));
	//m_SendEdit.SetFocus();
	GetDlgItem(IDC_BTN_SEND);
}


void CChatClientDlg::OnBnClickedButton1()
{
	// TODO: 在此添加控件通知处理程序代码
	CString m_strDate;
	CString m_strTime;
	CString m_strDateTime;
	CTime m_time;
	m_time = CTime::GetCurrentTime();             //获取当前时间日期  
	m_strDate = m_time.Format(_T("%x"));          //格式化日期  
	m_strTime = m_time.Format(_T("%X"));          //格式化时间  
	m_strDateTime = m_time.Format(_T("%Y-%m-%d %H:%M:%S %A"));   //格式化日期时间  
	//UpdateData(false);
	CFile file;
	char szChar[64] = "";
	file.Open(_T("Memorandum.txt"), CFile::modeReadWrite);
	::WideCharToMultiByte(CP_ACP, 0, L"备忘录", wcslen(L"备忘录"), szChar, 64, 0, 0);
	file.Write(szChar, strlen(szChar));
	file.SeekToEnd();
	file.Write("\r\n", 2);
	file.Write("\r\n", 2); 
	file.Write("\r\n", 2);
	//file.Write(_T("\r\n"), 2);
	file.Write(m_strDateTime, 20);
	//file.Write(_T("\r\n"),2);
	CString str;
	for (int i = 0; i < m_list1.GetItemCount(); i++)
	{
		if (m_list1.GetItemState(i, LVIS_SELECTED) == LVIS_SELECTED)
		{
			//int nSize = file.GetLength();
			//str += "\r\n";
			str = m_list1.GetItemText(i, 0);

			file.SeekToEnd();
			file.Write("\r\n", 2);
			//file.Write(_T("/r/n"),10);
			//::WideCharToMultiByte(CP_UTF8, 0, str, wcslen(str), szChar, 64, 0, 0);
			//file.Write(szChar, sizeof(szChar)/2);
			file.Write(str, str.GetLength());
			//AfxMessageBox(str);
		}
	}
	file.Close();
}


void CChatClientDlg::OnBnClickedButton2()
{
	// TODO: 在此添加控件通知处理程序代码
	CHistory m_H1;
	CStdioFile fsend;
	CString s_one;
	CString a;
	if (fsend.Open(_T("Memorandum.txt"), CFile::typeText | CFile::modeRead))
	{
		while (fsend.ReadString(s_one))
		{ 
			//s_one=
			//char szChar[64] = "";
			m_H1.m_Hiscontent += s_one;
			m_H1.m_Hiscontent += "\r\n";
			//::WideCharToMultiByte(CP_ACP, 0, s_one, wcslen(s_one), szChar, 64, 0, 0);
            // a += (CString)szChar;
			//m_H1.m_Hiscontent = a;
			/*CString CS;
			CS += "打算";*/
			//m_H1.m_Hiscontent = a;
		}
		m_H1.DoModal();
		//m_H1.m_EditofHis.SetWindowText(m_H1.m_Hiscontent);
	}
	fsend.Close();
}


void CChatClientDlg::OnBnClickedButton3()
{
	// TODO: 在此添加控件通知处理程序代码
	CMplayer dlg;
	CString strTemptxt = "111";
	SetDlgItemText(IDC_filename, strTemptxt);
	if (dlg.DoModal() == IDOK)
	{
		//extern 


		GetDlgItem(IDC_filename)->SetWindowText(strTemptxt);
		SetDlgItemText(IDC_filename, strTemptxt);
	}
}
